package com.gml.design.other.singleton;

/**
 * @ClassName : Singleton06
 * @Description : 懒汉式单例
 * 双重检查单例
 * @Author : gml
 * @Date: 2021-04-01 14:33
 */
public class Singleton06 {
    private static volatile Singleton06 INSTANCE;

    private Singleton06() {
    }

    public void say(){
        System.out.println("hello");
    }

    public static Singleton06 getInstance() throws InterruptedException {
        if (null == INSTANCE) {
            synchronized (Singleton06.class) {
                if (null == INSTANCE) {
                    Thread.sleep(100);
                    INSTANCE = new Singleton06();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Singleton06.getInstance().hashCode());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
